package com.wayin.lift.controller.logon;

import com.alibaba.fastjson.JSONObject;
import com.wayin.common.utils.PicUtils;
import com.wayin.common.utils.R;
import com.wayin.lift.entity.PersonnelEntity;
import com.wayin.lift.face.FaceCheck;
import com.wayin.lift.service.PersonnelService;
import com.wayin.lift.wechat.OpenIdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import static com.wayin.lift.face.FaceCheck.faceFeatures;
import static com.wayin.lift.socket.Server_Andriod.SendMessageToClient;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 登录注册接口
 *
 * @author HYXK
 * @email null@qq.com
 * @date 2020-05-26 09:56:38
 */
@Api(tags = "登录注册接口")
@RestController
@RequestMapping("/logOn")
public class LogOnController {

    @Autowired
    private PersonnelService personnelService;

    /**
     * 小程序登录
     */
    @ApiOperation("小程序登录")
    @RequestMapping(value = "/login", method = GET)
    public R login(String code){
        System.out.println(code);
        String openid = OpenIdUtil.oauth2GetOpenid(code);
        System.out.println(openid);
        PersonnelEntity p = personnelService.findByOpenId(openid);
        // 若无此用户则添加
        if (p == null){
            p = new PersonnelEntity();
            p.setOpenId(openid);
            p.setPassword("88888888");
            personnelService.insert(p);
            p = personnelService.findByOpenId(openid);
        }
        p.setPassword("********");
        return R.ok().put("page", p);
    }

    /**
     * 上传人脸
     */
    @ApiOperation("上传人脸")
    @RequestMapping(value = "send", method = POST)
    public R send(@RequestParam("openId") String openId, @RequestParam("file") MultipartFile file) throws Exception {
        if (file != null){
            String path = new PicUtils().putPic(file);
            String path1 = ClassUtils.getDefaultClassLoader().getResource("static/face").getPath();
            File face2 = new File(path1 + "/" + path);
            List<PersonnelEntity> personnel = personnelService.findAllPersonnel();
            if (personnel.size() != faceFeatures.size()){
                FaceCheck.face4(personnel);
            }
            PersonnelEntity p;
            try {
                p = FaceCheck.face5(personnel, face2);
                if (p == null){
                    return R.error("检测不到人脸！");
                } else {
                    return R.error("人脸已存在");
                }
            } catch (Exception e){
                if (e.getMessage().equals("noFace")){
                    p = new PersonnelEntity();
                    p.setPicture(path);
                    p.setOpenId(openId);
                    if (personnelService.updateByOpenId(p)){
                        personnel = personnelService.findAllPersonnel();
                        if (personnel.size() != faceFeatures.size()){
                            FaceCheck.face4(personnel);
                        }
                        // 发送消息给Android提醒其更新人脸库
                        SendMessageToClient("{\"openId\":\"" + openId + "\",\"picture\":\"" + path + "\",\"name\":\"" + p.getName() + "\"}");
                        return R.ok().put("path", path);
                    } else {
                        return R.error("上传失败！");
                    }
                }
            }
            return R.error("上传失败！");
        }
        return R.error("图片不能为空！");
    }

    /**
     * 后台登录
     */
    @ApiOperation("后台登录")
    @RequestMapping(value = "/backLogin", method = GET)
    public R backLogin(String username, String password){
        PersonnelEntity p = personnelService.backLogin(username, password);
        if (p == null){
            return R.error("账号或密码错误！");
        }
        p.setPassword("********");
        return R.ok().put("page", p);
    }

}
